<template>
  <div class="login-container">

    <el-image class="form-sider" src="/static/img/banner.png" />

    <div class="form-box">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">{{ $t('login.title') }}</h3>
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            :placeholder="$t('login.username')"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            :placeholder="$t('login.password')"
            name="password"
            tabindex="2"
            auto-complete="on"
            clearable
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-form-item prop="captcha_code" class="captchaCodeImgBar">
          <span class="svg-container">
            <i class="awesome-icon fas fa-shield-alt" />
          </span>
          <el-input
            ref="captcha_code"
            v-model="loginForm.captcha_code"
            :placeholder="$t('login.captchaCode')"
            name="captcha_code"
            type="text"
            clearable
            @keyup.enter.native="handleLogin"
          />
          <el-image
            :src="captchaCodeImg"
            :title="$t('login.captchaCode_change_tips')"
            @click="getCaptcha"
          >
            <div slot="error">
              <i class="el-icon-loading" />
            </div>
          </el-image>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          @click.native.prevent="handleLogin"
        >
          {{ $t('login.login') }}
        </el-button>
      </el-form>
    </div>
    <el-row class="copyright">
      <el-col :offset="0"><span v-html="$t('jishupu.copyright')" /></el-col>
    </el-row>
  </div>
</template>

<script>
import i18n from '@/lang'
import { getCaptcha } from '@/api/captcha'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'demo',
        password: 'demo',
        captcha_code: '',
        captcha_id: ''
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: i18n.t('login.username_tips')
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: i18n.t('login.password_tips')
          }
        ],
        captcha_code: [
          {
            required: true,
            message: i18n.t('login.captchaCode_tips'),
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      captchaCodeImg: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((response) => {
              this.$message.success(response.message)
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.getCaptcha()
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async getCaptcha() {
      const { data } = await getCaptcha()
      this.captchaCodeImg = data.img
      this.loginForm.captcha_id = data.captcha_id
    }
  }
}
</script>

<style lang="scss">
@import '@/styles/variables.scss';
$cursor: white;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px white inset !important;
      }
    }
  }

  .el-button {
    width: 100%;
    font-size: 16px;
  }

  .el-form-item {
    border: 1px solid $border-color-base;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
$dark_gray: #889aa4;

@media screen and (max-width: 900px) {
    .form-sider {
      display: none;
    }
}

@media screen and (max-width: 450px) {
     .login-form {
      width: 90%;
      margin: 0 auto
    }
}

.login-container {
  height: 100%;
  width: 100%;
  background: $background-color-extra;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  -webkit-align-items: center;

  .form-sider {
    height: 410px;
    overflow: hidden;
    width: 450px;
  }

  .form-box{
    width: 450px;
    height: 410px;
    background-color: white;
  }

  .login-form {
    position: relative;
    overflow: hidden;
    padding: 45px;
  }

  .captchaCodeImgBar {
    .el-input {
      width: 65%;
    }
    .el-image {
      height: 31px;
      width: 25%;
      position: absolute;
      top: 8px;
      right: 10px;
      cursor: pointer;
      text-align: right;
      color: white;
      .el-icon-loading {
        color:$color-text-regular
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 24px;
      margin: 0px auto 25px auto;
      text-align: left;
      font-weight: normal;
      letter-spacing: 2px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .copyright {
    position: fixed;
    bottom:0;
    line-height: 40px;
    text-align: center;
    padding: 20px 0;
  }
}
</style>
